package com.alongstudio.account.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alongstudio.account.action.base.BaseAction;
import com.alongstudio.account.commons.exception.BusinessException;
import com.alongstudio.account.constants.IViewConstant;
import com.alongstudio.account.constants.SystemConstant;
import com.alongstudio.account.entity.Category;
import com.alongstudio.account.entity.ClientUser;
import com.alongstudio.account.service.ICategoryService;
import com.alongstudio.account.vo.CategoryVo;

@SuppressWarnings("serial")
@Component("CategoryAction")
@Scope("prototype")
public class CategoryAction extends BaseAction {
	@Autowired
	@Qualifier("CategoryService")
	private ICategoryService categoryService;
	private List<CategoryVo> categoryList = new ArrayList<CategoryVo>();
	private CategoryVo category;
	private String OPERATOR_RESULT;// 返回提示结果
	private String categoryName;
	private String parentId;// 父节点id
	// private String type;// 类型
	private String id;
	private Map categoryMap = new HashMap();
	@Override
	public String executeAction() throws BusinessException {
		return SUCCESS;
	}
	/**
	 * 获取所有支出分类
	 * @return
	 * @throws BusinessException
	 * String
	 */
	public String getAllExpenditureCategoryForSettings() throws BusinessException {
		categoryList = categoryService.getFirstClassCategory(getCurrentClientUserId(),SystemConstant.CATEGORY_TYPE_EXPENDITURE);
		categoryMap = categoryService.getSubCategoryMapForSettings(getCurrentClientUserId());
		return SUCCESS;
	}
	
	/**
	 * 获取第一层支出类别
	 * @return
	 * @throws BusinessException
	 * String
	 */
	public String getFirstClassExpenditureCategory() throws BusinessException{
		categoryList = categoryService.getFirstClassCategory(getCurrentClientUserId(),SystemConstant.CATEGORY_TYPE_EXPENDITURE);
//		total = categoryService.
		return SUCCESS;
	}
	/**
	 * 新建支出类别
	 * @return
	 * @throws BusinessException
	 * String
	 */
	public String newFirstClassExpenditureCategory()throws BusinessException {
		boolean isOk = false;
		if (categoryName != null) {
			CategoryVo cateVo = new CategoryVo();
			cateVo.setCategoryName(categoryName);
			cateVo.setGrade(SystemConstant.TREE_GRADE_FIRST);
			cateVo.setHasChild(SystemConstant.HAS_CHILD_NO);
			// cate.setSortNum(SystemConstant.SORT_NUM_INIT);
			cateVo.setDelTag(SystemConstant.DEL_TAG_NOT_DELETE);
			cateVo.setType(SystemConstant.CATEGORY_TYPE_EXPENDITURE);
			cateVo.setClientUserId(getCurrentClientUserId());
			cateVo.setCreateTime(new Date());
			String id = categoryService.createCategory(cateVo);
			isOk = id != null && !id.isEmpty();
		}
		OPERATOR_RESULT = isOk
				? IViewConstant.SAVE_SUCCESS
				: IViewConstant.SAVE_FAILED;
		return SUCCESS;
	
	}
	
	public ICategoryService getCateService() {
		return categoryService;
	}
	public void setCateService(ICategoryService cateService) {
		this.categoryService = cateService;
	}
	public List<CategoryVo> getCategoryList() {
		return categoryList;
	}
	public void setCategoryList(List<CategoryVo> categoryList) {
		this.categoryList = categoryList;
	}
	public CategoryVo getCategory() {
		return category;
	}
	public void setCategory(CategoryVo category) {
		this.category = category;
	}
	public String getOPERATOR_RESULT() {
		return OPERATOR_RESULT;
	}
	public void setOPERATOR_RESULT(String oPERATOR_RESULT) {
		OPERATOR_RESULT = oPERATOR_RESULT;
	}
	public String getCategoryName() {
		return categoryName;
	}
	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}
	public String getParentId() {
		return parentId;
	}
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public Map getCategoryMap() {
		return categoryMap;
	}
	public void setCategoryMap(Map categoryMap) {
		this.categoryMap = categoryMap;
	}
	
	
}

